package com.xuanwu.ai.flow.controller;

import com.alibaba.fastjson.JSONObject;
import com.xuanwu.ai.core.domain.ErrorData;
import com.xuanwu.ai.flow.dto.CommonContextDTO;
import com.xuanwu.ai.flow.mapper.FlowManageMapper;
import com.xuanwu.ai.flow.model.FlowChain;
import com.xuanwu.ai.flow.service.FlowDataServiceImpl;
import com.xuanwu.ai.flow.utils.ThreadSessionUtil;
import com.xuanwu.ai.web.views.ResponseVo;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(value = "AI流程聚合接口", produces = "application/json")
@RestController
@Slf4j
public class AIController {

    @Resource
    private FlowExecutor flowExecutor;

    @Autowired
    private FlowManageMapper flowManageMapper;

    @Autowired
    private FlowDataServiceImpl flowDataService;

    @PostMapping("/ai/skuDetect")
    @ApiOperation(value = "SKU识别", notes = "SKU识别")
    public ResponseVo<JSONObject> skuDetect(@RequestBody CommonContextDTO commonContextDTO) {
        try {
            LiteflowResponse response = flowExecutor.execute2Resp("skuChain", "", commonContextDTO);
            if (!response.isSuccess()) {
                return ResponseVo.fail("500", new ErrorData(500, response.getMessage()));
            }
            CommonContextDTO customContext = response.getContextBean(CommonContextDTO.class);
            return new ResponseVo<>(customContext.getNodeJsonResult());
        } catch (Exception e) {
            return ResponseVo.fail("500", new ErrorData(500, e.getMessage()));
        }
    }


    @PostMapping("/ai/{chainId}")
    @ApiOperation(value = "识别聚合接口", notes = "识别聚合接口")
    public ResponseVo<JSONObject> aiGeneralDetect(@PathVariable Long chainId, @RequestBody CommonContextDTO commonContextDTO) {
        LiteflowResponse response = null;
        try {
            FlowChain flowChain = flowManageMapper.selectById(chainId);
            if (ObjectUtils.isEmpty(flowChain)) {
                return ResponseVo.fail("500", new ErrorData(500, "识别接口不存在!"));
            }
            if (!flowChain.getTenantId().equals(ThreadSessionUtil.get().getTenantId())) {
                return ResponseVo.fail("500", new ErrorData(500, "租户没有创建相关流程!"));
            }
            commonContextDTO.setFlowId(flowChain.getId());
            commonContextDTO.setFlowVersionId(flowChain.getFlowVersionId());
            response = flowExecutor.execute2Resp(flowChain.getChainName(), "", commonContextDTO);
            if (commonContextDTO.getTenantId() == null) {
                commonContextDTO.setTenantId(String.valueOf(flowChain.getTenantId()));
            }
            if (!response.isSuccess()) {
                return ResponseVo.fail("500", commonContextDTO.getNodeJsonResult(), String.format("流程%s执行出错,失败原因为%s", flowChain.getChainName(), response.getMessage()));
            }
            return new ResponseVo<>(commonContextDTO.getNodeJsonResult());
        } catch (Exception e) {
            return ResponseVo.fail("500", new ErrorData(500, e.getMessage()));
        } finally {
            flowDataService.saveFlowData(response);
        }
    }

}
